package com.luojun.dao;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.query.Query;

import com.luojun.entity.Car;
import com.luojun.util.HibernateUtil;

public class CarDao {
	Session session = null;
	/**
	 * 添加方法
	 * @param car
	 */
	public void save(Car car){
		session = HibernateUtil.getCurrentSession();
		session.save(car);
	}
	
	public void update(Car car){
		session = HibernateUtil.getCurrentSession();
		session.update(car);
	}
	/**
	 * 删除方法
	 * @param ID
	 */
	public void del(int userid,int commodityid){
		session = HibernateUtil.getCurrentSession();
		Query query = session.createQuery("delete from Car c where c.Commodity = ? and c.User = ?");
		query.setInteger(0,commodityid);
		query.setInteger(1,userid);
		query.executeUpdate();
		
	}
	/**
	 * 买一个
	 * @param commodityId
	 */
	public void mai(int commodityId){
		session = HibernateUtil.getCurrentSession();
		Query query= session.createQuery("update Car c set c.quantity=0 where c.Commodity=?");
		query.setInteger(0,commodityId);
		query.executeUpdate();
	}

	/**
	 * 查找
	 * @param userId
	 * @return
	 */
	public List<Car> find(int userId){
		session = HibernateUtil.getCurrentSession();
		Query query= session.createQuery("from Car c where c.User=? and c.quantity = 1");
		query.setInteger(0, userId);
		List<Car> list = query.list();
		return list;
	}
	
	/**
	 * 查找
	 * @param userId
	 * @return
	 */
	public List<Car> findsell(int userId){
		session = HibernateUtil.getCurrentSession();
		Query query= session.createQuery("from Car c where c.User=? and c.quantity = 0");
		query.setInteger(0, userId);
		List<Car> list = query.list();
		return list;
	}
	
	/**
	 * 查找
	 * @param userId
	 * @return
	 */
	public List<Car> findbycommodityID(int commodityId){
		session = HibernateUtil.getCurrentSession();
		Query query= session.createQuery("from Car c where c.Commodity=? and c.quantity = 1 ");
		query.setInteger(0, commodityId);
		List<Car> list = query.list();
		return list;
	}
}
